﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Warehouse.Web.AppCode;
using System.Web.Script.Serialization;

namespace Warehouse.Web.Inventory
{
    public partial class MoveList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                JavaScriptSerializer ser = new JavaScriptSerializer();

                base.RegisterClientScriptContent("allproducts", ser.Serialize(DataAccess.Product.GetProducts()));
                base.RegisterClientScriptContent("allproductspecifications", ser.Serialize(DataAccess.ProductSpecification.GetProductSpecifications()));
                base.RegisterClientScriptContent("allwarehouses", ser.Serialize(DataAccess.Warehouse.GetWarehouses()));
                base.RegisterClientScriptContent("allsuppliers", ser.Serialize(DataAccess.Supplier.GetSuppliers()));
            }
        }
        protected override void ProcessAjaxRequest(HttpContext context, string action)
        {
            switch (action)
            {
                case "Search":
                    {
                        int fromHouseId = int.Parse(context.Request.QueryString["FromHouseId"]);
                        int toHouseId = int.Parse(context.Request.QueryString["ToHouseId"]);
                        DateTime dtStart = DateTime.Parse(context.Request.QueryString["StartDate"]);
                        DateTime dtEnd = DateTime.Parse(context.Request.QueryString["EndDate"]);

                        WebUtil.WriteJsonResult(context, DataAccess.MovingStock.Search(fromHouseId, toHouseId, dtStart.Date, dtEnd.Date.AddDays(1)).OrderByDescending(p => p.MovingDate));
                    }
                    break;
                case "GetStock":
                    {
                        Guid stockId = new Guid(context.Request.QueryString["StockID"]);
                        var stock = DataAccess.MovingStock.GetStock(stockId);
                        if (stock != null)
                        {
                            WebUtil.WriteJsonResult(context, new { Stock = stock, Details = DataAccess.MovingDetail.GetDetails(stock.ID) });
                        }
                        else
                        {
                            WebUtil.WriteJsonResult(context, null);
                        }
                    }
                    break;
                default:
                    base.ProcessAjaxRequest(context, action);
                    break;
            }
        }
    }
}